<?php 
	require_once  __DIR__."/../silex/vendor/autoload.php";
	require_once  __DIR__."/Helpers/session.php";
	require_once  __DIR__."/Helpers/connection.php";
	
	use Helpers\Session;
	
	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\HttpFoundation\Response;


	$app = new Silex\Application();
	$app['debug']=true;
	$app->register(new Silex\Provider\DoctrineServiceProvider(), Helpers\Connection\getCon('local'));
	$app->register(new Silex\Provider\SessionServiceProvider());
	$app->register(new Silex\Provider\TwigServiceProvider(), array(
	    'twig.path' => __DIR__.'/../views',
	));
	$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
	$app->register( new Silex\Provider\HttpFragmentServiceProvider() );



	

	$app['controllers']
		->before(function() use ($app) {
			if( !$app['session']->has('uid') ){
				Session\session_init($app);	
			}	

			$uid = 	$app['session']->get('uid');
			$sid = $app['session']->getid();
			$app['session']->set('uid',1);

			$sql_exist = 'SELECT COUNT(*) count FROM session WHERE session_id = ?';
			$res_exist = $app['db']->fetchAssoc($sql_exist, array( (string) $sid ));
			//echo "count ". $res_exist['count']. "<br>";
			
			if($res_exist['count'] == 0){
				$app['db']->delete( 'session', array( 'session_id' => $sid) );
				Session\session_init($app);	
			}



			if($app['session']->get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']){
				$app['db']->delete( 'session', array( 'session_id' => $sid) );
				Session\session_init($app);		
			}

			if($uid > 0){
				$sql = "SELECT TIME_TO_SEC(TIMEDIFF(now(),session_time))/60 time FROM `session` WHERE session_id = ? ";
				//echo "sql $sql <br>";
				$res = $app['db']->fetchAssoc($sql, array( (string) $sid ));
				//echo "time {$res['time']} <br>";
				if(	  $res['time'] > 60  ){
				//	echo ">0 <br>";
					//Session\session_destroy($app);	
					$app['db']->delete( 'session', array( 'session_id' => $sid) );
					Session\session_init($app);	
					//Session\session_update($app);
				}else{
				//	echo "<60  <br>";
					Session\session_update($app);
				}
			}

		});
	
	$app['homepage_route'] = Helpers\Connection\getHomeRoute();

	$app->get('/', function() use ($app) {
			return $app['twig']->render('index.html');
	})
	->before(function() use ($app){
			$uid = $app['session']->get('uid');
			if($uid == 0){
				return $app->redirect($app['url_generator']->generate('login'));
			}
		})
	->bind('homepage');

	
	$app->get('/login', function() use ($app) {
		return $app['twig']->render('login.html');
	})->bind("login");

	$app->post('/login', function(Request $request) use ($app){
		$login = '';
		$pass = '';
		$login = $request->get('login');
		$pass = $request->get('password');
		$sql = 	"SELECT iduser user_id FROM  user WHERE user_login = ? AND	 user_pass = md5( ? )";
		$res = $app['db']->fetchAssoc($sql, array( (string) $login, (string) $pass ) );

		if( !$res ){
			return $app->redirect('/series/login');
		}else{
			$sql = 	"SELECT usersetting_role role FROM  user_setting WHERE user_id = ?";
			$res_setting = $app['db']->fetchAssoc($sql, array(  (int) $uid ) );			
			$app['session']->set('uid',$res['user_id']);
			$app['session']->set('REMOTE_ADDR',$_SERVER['REMOTE_ADDR']);
			if(!$res_setting){
				$app['session']->set('role', $res_setting['role'] );
			}else{
				$app['session']->set('role', 6 );
			}
			Session\session_update($app);
			return $app->redirect($app['url_generator']->generate('homepage'));
		}
	});


	/**
	example mount
	**/
	/*
	$sapp->mount("/films", include __DIR__."/Controllers/series.php");
	$app->mount("/users", include __DIR__."/Controllers/user.php");
	$app->mount("/usersko", include __DIR__."/Controllers/userko.php");
	*/

	return $app;
?>